package com.situ.cart.api.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.situ.cart.spi.model.CartItem;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface CartItemMapper extends BaseMapper<CartItem> {
    default List<CartItem> findBy(CartItem ci) {
        LambdaQueryWrapper<CartItem> qw = Wrappers.lambdaQuery();
        if (ci.getUserId() != null) {
            qw.eq(CartItem::getUserId, ci.getUserId());
        }
        if (ci.getProductId() != null) {
            qw.eq(CartItem::getProductId, ci.getProductId());
        }
        return selectList(qw);
    }
    @Select("SELECT user_id FROM cart WHERE product_id = #{id}")
    int[] findByProductId(Integer id);

    @Delete("DELETE FROM cart where user_id=#{userId}")
    int clearAllByUserId(Integer userId);
}
